#include "my_basic_timer.h"
#include "sys_param.h"
#include "HF_power.h"



static TIM_TimeBaseInitType TIM_TimeBaseStructure;
static OCInitType TIM_OCInitStructure;
static uint16_t PrescalerValue = 0;

#define PERIOD_25KHZ_TO_8MHZ       (320 - 1)

typedef struct HF_power
{
    /* data */
    LEVEL_e level;
    int power_duty;
}HF_POWER_PARAM_t;


HF_POWER_PARAM_t hf_power_data[] = {
    {LEVEL_0, 0},
    {LEVEL_1, 10},
    {LEVEL_2, 20},
    {LEVEL_3, 30},
    {LEVEL_4, 40},
    {LEVEL_5, 50},
    {LEVEL_6, 60},
    {LEVEL_7, 70},
    {LEVEL_8, 80},
    {LEVEL_9, 90},
    {LEVEL_10, 100},

};


/**
 * @brief  Configures tim3 clocks.
 * 
 * 
 * TIM1 frequency = TIM1 counter clock / (TIM1_Period + 1)/(Prescaler + 1)
 *  duty cycle is equal to: TIM1_CCR1/(TIM1_ARR + 1) = 50%
 */
static void hf_voltage_ctrl_timer_cfg(void)
{
 /* Compute the prescaler value */
    PrescalerValue = 8-1; //(uint16_t)(SystemCoreClock / 12000000) - 1;
    // PrescalerValue =1 (uint16_t)(SystemCoreClock / 10800000) - 1;; //(uint16_t)(SystemCoreClock / 12000000) - 1;
    /* Time base configuration */
    TIM_InitTimBaseStruct(&TIM_TimeBaseStructure);    
    TIM_TimeBaseStructure.Period    = PERIOD_25KHZ_TO_8MHZ;
    TIM_TimeBaseStructure.Prescaler = PrescalerValue;
    TIM_TimeBaseStructure.ClkDiv    = 0;
    TIM_TimeBaseStructure.CntMode   = TIM_CNT_MODE_UP;

    //-------------timer 9 ch1    
    TIM_InitTimeBase(TIM9, &TIM_TimeBaseStructure);
    /* PWM1 Mode configuration: Channel1 */
    TIM_InitOcStruct(&TIM_OCInitStructure);    
    TIM_OCInitStructure.OcMode      = TIM_OCMODE_PWM1;
    TIM_OCInitStructure.OutputState = TIM_OUTPUT_STATE_ENABLE;
    // TIM_OCInitStructure.Pulse       = PERIOD_25KHZ_TO_8MHZ *30/100; //CCR1_Val; ---hf 变压器升压前为10v
    TIM_OCInitStructure.Pulse       = PERIOD_25KHZ_TO_8MHZ *40/100; //2021 10 17 改为40%，防止hf 电压过高损坏器件

    TIM_OCInitStructure.OcPolarity  = TIM_OC_POLARITY_HIGH;

    TIM_InitOc1(TIM9, &TIM_OCInitStructure);
    TIM_ConfigOc1Preload(TIM9, TIM_OC_PRE_LOAD_ENABLE);
    TIM_Enable(TIM9, ENABLE);
}


void hf_voltage_restar_duty(int duty)
{
  /* TIM2 enable counter */
	TIM9->CCDAT1 = PERIOD_25KHZ_TO_8MHZ*(100 - duty)/100;
}


/*********************************************
PB0------IHF_ADC           // 高周波--负载检测 ---------[ADC_IN9]
PB1------HF_VCC_ADC        // 高周波电压 检测  ---------[ADC_IN10]

PB15------HF_VCC_EN        //
PB10------HF_SW_EN      
PB12------HF_VCC_PWM        //------------------[TIM9_CH1]
**********************************************/
static void hf_power_gpio_init(void)
{
	  GPIO_InitType GPIO_InitStructure;
	
    gpio_output_cfg(HF_OUTPUT_CTRL_PORT, HF_OUTPUT_CTRL_PIN);
    gpio_output_cfg(HF_POWER_CTRL_PORT, HF_POWER_CTRL_PIN);

    //--------VCC_EN pin  PB15
    // time9 ---ch1---af1, 频率为25khz【避开人耳听到的范围】
    //time 4


    GPIO_InitStruct(&GPIO_InitStructure);
    /* GPIOA Configuration:TIM9 Channel1, as alternate function push-pull */
    GPIO_InitStructure.Pin        = HF_VOLTAGE_CTRL_PIN;
    GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Current = GPIO_DC_4mA;
    GPIO_InitStructure.GPIO_Alternate = GPIO_AF1_TIM9;
    GPIO_InitPeripheral(HF_VOLTAGE_CTRL_PORT, &GPIO_InitStructure);

    // 开时钟
    RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM9, ENABLE);
    hf_voltage_ctrl_timer_cfg();
}

void hf_voltage_ctrl(void)
{



}

void hf_power_on(void)
{
    HF_POWER_ON();
}

void hf_power_off(void)
{
    HF_POWER_OFF();
}

void hf_output_enable(void)
{
    HF_OUTPUT_ENABLE();
}

void hf_output_disable(void)
{
    HF_OUTPUT_DISABLE();
}


void my_hf_power_init(void)
{
    hf_power_gpio_init();
    hf_power_off();
    hf_output_disable();
}

